package vashion.azeroth.core.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * Created by xiaonan on 15/9/6.
 */
public class OSSConfigure {
	private String endpoint;
	private String accessKeyId;
	private String accessKeySecret;
	private String bucketName;
	private String accessUrl;
	OSSClient ossClient = null;
	public OSSConfigure() {

	}

	/**
	 * 通过配置文件.properties文件获取，这几项内容。
	 *
	 * @param storageConfName
	 * @throws IOException
	 */
	public OSSConfigure(String storageConfName) throws IOException {

		Properties prop = new Properties();
		prop.load(this.getClass().getClassLoader()
				.getResourceAsStream(storageConfName));

		endpoint = prop.getProperty("endpoint").trim();
		accessKeyId = prop.getProperty("accessKeyId").trim();
		accessKeySecret = prop.getProperty("accessKeySecret").trim();
		bucketName = prop.getProperty("bucketName").trim();
		accessUrl = prop.getProperty("accessUrl").trim();
		ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);

	}

	public OSSConfigure(String endpoint, String accessKeyId,
						String accessKeySecret, String bucketName, String accessUrl) {

		this.endpoint = endpoint;
		this.accessKeyId = accessKeyId;
		this.accessKeySecret = accessKeySecret;
		this.bucketName = bucketName;
		this.accessUrl = accessUrl;
	}

	public String getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public String getAccessKeyId() {
		return accessKeyId;
	}

	public void setAccessKeyId(String accessKeyId) {
		this.accessKeyId = accessKeyId;
	}

	public String getAccessKeySecret() {
		return accessKeySecret;
	}

	public void setAccessKeySecret(String accessKeySecret) {
		this.accessKeySecret = accessKeySecret;
	}

	public String getBucketName() {
		return bucketName;
	}

	public void setBucketName(String bucketName) {
		this.bucketName = bucketName;
	}

	public String getAccessUrl() {
		return accessUrl;
	}

	public void setAccessUrl(String accessUrl) {
		this.accessUrl = accessUrl;
	}

	public OSSClient getOssClient() {
		return ossClient;
	}

	public static void main(String[] args){
		try {
			OSSConfigure ossConfigure = new OSSConfigure("ossconfig.properties");
			OSSClient ossClient = ossConfigure.getOssClient();
			List<Bucket> accessControlList = ossClient.listBuckets();
			boolean result = ossClient.doesBucketExist(ossConfigure.getBucketName());
			System.out.println(accessControlList);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
